package com.ffms.servlet;

import com.ffms.dao.impl.MoneyDaoImpl;
import com.ffms.entity.FamilyMember;
import com.ffms.entity.Money;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
//存取款
@WebServlet("/AllMoneyServlet")
public class AllMoneyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取用户请求数据
        String flag = request.getParameter("flag");
        String aflag = flag;
        if (flag.equals("0")) {
            flag = "0,1";//存取款记录
        } else if (flag.equals("1")) {
            flag = "2,3";//借还钱记录
        }
        List<Money> monies;
        Object user = request.getSession().getAttribute("user");
        MoneyDaoImpl moneyDaoImpl = new MoneyDaoImpl();

        if (user instanceof FamilyMember) {
            FamilyMember familyMember = (FamilyMember) user;
            monies = moneyDaoImpl.findMoneyByMid(flag, familyMember.getSid());
        } else {
            monies = moneyDaoImpl.findMoney(flag);
        }


        request.setAttribute("monies", monies);
        request.setAttribute("flag", aflag);
        request.getRequestDispatcher("admin/allMoneyList.jsp?flag=" + aflag).forward(request, response);
    }
}
